package com.apache.bid.service.adminplugins;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.entity.BidLog;
import com.apache.bid.manager.BidLogManager;
import com.apache.cache.util.Validator;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;

public class SaveBidLogPluginImpl implements PluginConnector {

	protected Logger log = Logger.getLogger(getClass());
	protected BidLogManager bidLogManager;

	@Override
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();
		result.setResult("false");

		try {
			String sessionId = String.valueOf(vo.getParams("sessionId"));
			String userId = String.valueOf(vo.getParams("userId"));
			String logName = String.valueOf(vo.getParams("logName"));
			String logContent = String.valueOf(vo.getParams("logContent"));
			String createTime = DateUtils.getNow();
			String whoUserName = String.valueOf(vo.getParams("userName"));
			String orgCname = String.valueOf(vo.getParams("orgCname"));
			String objectType = String.valueOf(vo.getParams("objectType"));
			String objectId = String.valueOf(vo.getParams("objectId"));
			String objectName = String.valueOf(vo.getParams("objectName"));

			String ip = String.valueOf(vo.getParams("ip"));

			//验证必填项
			if (Validator.isNull(userId) && Validator.isNull(whoUserName)) {
				result.setMessage("操作用户id或操作用户账号不能为空！");
				return result;
			}
			if (Validator.isNull(ip)) {
				result.setMessage("IP地址不能空！");
				return result;
			}
			if (Validator.isNull(logName)) {
				result.setMessage("日志名称不能空！");
				return result;
			}
			if (Validator.isNull(logContent)) {
				result.setMessage("日志内容不能空！");
				return result;
			}
			if (Validator.isNull(objectId) && Validator.isNull(objectType)) {
				result.setMessage("关联的业务id或业务名称不能为空！");
				return result;
			}

			BidLog log = new BidLog();
			log.setUserId(userId);
			log.setWhoUserName(whoUserName);
			log.setLogName(logName);
			log.setLogContent(logContent);
			log.setCreateTime(createTime);
			log.setOrgCname(orgCname);
			log.setObjectType(objectType);
			log.setObjectId(objectId);
			log.setObjectName(objectName);
			log.setSessionId(sessionId);
			log.setIp(ip);

			vo.setObj(log);

			String resultStr = bidLogManager.saveInfo(vo);
			if (StrUtil.isNotNull(resultStr)) {
				result.setMessage("操作成功！");
				result.setResult("true");
				result.setEntity(resultStr);
			} else {
				result.setMessage("操作失败");
			}
		} catch (Exception e) {
			result.setMessage("操作失败");
		}
		return result;
	}

	public void setBidLogManager(BidLogManager bidLogManager) {
		this.bidLogManager = bidLogManager;
	}

}
